package itbour.onetouchshow.activity.userinfo;


import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import itbour.onetouchshow.R;
import itbour.onetouchshow.base.mvp.MVPBaseActivity;
import itbour.onetouchshow.bean.eventbus.UpdateUserAction;
import itbour.onetouchshow.listener.EdittextListener;
import itbour.onetouchshow.singlecase.InchingDataSingleCase;
import itbour.onetouchshow.utils.T_;

import static itbour.onetouchshow.bean.eventbus.UpdateUserAction.UPDATA_USER_NAME;
import static itbour.onetouchshow.bean.eventbus.UpdateUserAction.UPDAT_USER_SGIN;


/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class UpdateActivity extends MVPBaseActivity<UserinfoContract.View, UserinfoPresenter> implements UserinfoContract.View {

    @BindView(R.id.up_edit)
    EditText upEdit;
    @BindView(R.id.id_tv_title)
    TextView titleTv;
    String titleContent;
    private int UPDATA_KEY = 1;


    @Override
    public void loadSucceed(String result) {
        UpdateUserAction updateUserAction = new UpdateUserAction();
        switch (UPDATA_KEY) {
            case UPDAT_USER_SGIN:
                updateUserAction.setSubType(UPDAT_USER_SGIN);
                break;
            case UPDATA_USER_NAME:
                updateUserAction.setSubType(UPDATA_USER_NAME);
                break;

        }
        updateUserAction.setContent(upEdit.getText().toString());
        EventBus.getDefault().post(updateUserAction);
        finish();
    }

    @Override
    public void loadFaild(String error) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_update_source;
    }

    @Override
    protected void afterCreate(Bundle savedInstanceState) {
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            UPDATA_KEY = extras.getInt("subType", 0);
            titleContent = extras.getString("content");
        }
        if (!TextUtils.isEmpty(titleContent)) {
            upEdit.setText(titleContent);
            upEdit.setSelection(titleContent.length());//将光标移至文字末尾
        }
        switch (UPDATA_KEY) {
            case UPDATA_USER_NAME:
                titleTv.setText("昵称");
                upEdit.setFilters(new InputFilter[]{new InputFilter.LengthFilter(InchingDataSingleCase.INSTANCE.getLengthControl().getNickNameMaxLen())});
                upEdit.setHint("请输入昵称");
                break;
            case UPDAT_USER_SGIN:
                titleTv.setText("个性签名");
                upEdit.setFilters(new InputFilter[]{new InputFilter.LengthFilter(InchingDataSingleCase.INSTANCE.getLengthControl().getSignMaxLen())});
                upEdit.setHint("请输入个性签名");
                break;
        }

    }

    @Override
    protected void initTitle() {
        super.initTitle();
        setTitleText("修改资料");
        getToolBarView().getLeftimageView().setVisibility(View.GONE);
        getToolBarView().getLeftTextView().setVisibility(View.VISIBLE);
        getToolBarView().getLeftTextView().setText("取消");
        getToolBarView().getLeftTextView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        getToolBarView().getRightTextView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String input = upEdit.getText().toString();
                if (TextUtils.isEmpty(input)) {
                    T_.showToastReal("请认真填写哟");
                    return;
                }
                switch (UPDATA_KEY) {
                    case UPDAT_USER_SGIN:
                        mPresenter.upDateUserInfo("", input, "");
                        break;
                    case UPDATA_USER_NAME:
                        mPresenter.upDateUserInfo(input, "", "");
                        break;

                }
            }
        });

        upEdit.addTextChangedListener(new EdittextListener() {
            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() != 0 && s.toString() != null) {
                    getToolBarView().getRightTextView().setVisibility(View.VISIBLE);
                    getToolBarView().getRightTextView().setText("确定");
                } else {
                    getToolBarView().getRightTextView().setVisibility(View.GONE);
                }
            }
        });
    }
}
